package com.g.code.test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.StringTokenizer;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class SHA256 {
    private static String secret = "key";
    private static String check_info = "97yD9DBThCSxMpjmqm-xQ-9NWaFJRhdZl0edvC0aPNg.The quick brown fox jumps over the lazy dog";

    public static void main(String[] argc) {
        System.out.println(isChecked(secret, check_info));
        // String str=SHA256.getStr(secret, check_info.split("[.]")[1]);
    }

    public static boolean isChecked(String secret, String Value) {
        if (Value == null) {
            System.out.println("ERROR: Unable to retrieve signed_request parameter");
            return false;
        }
        int count = 0;
        String payload = null;
        StringTokenizer st = new StringTokenizer(Value, ".");
        String check_key = "";
        while (st.hasMoreTokens()) {
            if (count == 1) {
                payload = st.nextToken();
                break;
            } else if (count == 0) {
                check_key = st.nextToken();
            } else {
                st.nextToken();
            }
            count++;
        }
        String check_key_value = "";
        try {
            for (final byte element : base64_url_decode(check_key)) {
                check_key_value += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return check_key_value.equals(getStr(secret, payload));
    }

    public static String getStr(String key, String info) {
        final Charset asciiCs = Charset.forName("US-ASCII");
        // final Charset asciiCs = Charset.forName("UTF-8");
        Mac sha256_HMAC = null;
        // String KEYCODE="HMACSHA1";
        String KEYCODE = "HMACSHA256";
        try {
            sha256_HMAC = Mac.getInstance(KEYCODE);
            final SecretKeySpec secret_key = new SecretKeySpec(asciiCs.encode(key).array(), KEYCODE);
            sha256_HMAC.init(secret_key);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final byte[] mac_data = sha256_HMAC.doFinal(asciiCs.encode(info).array());
        String result = "";
//         try {
//         System.out.println(new String(base64_url_eecode(mac_data)));
//         } catch (IOException e) {
//         // TODO Auto-generated catch block
//         e.printStackTrace();
//         }
        for (final byte element : mac_data) {
            result += Integer.toString((element & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }

    public static byte[] base64_url_decode(String input) throws IOException {
        return new Base64(true).decode(input);
    }

    public static byte[] base64_url_eecode(byte[] input) throws IOException {
        return new Base64(true).encode(input);
    }
}
